package com.ctgu.rabbitmq.rabbitmq.queue.messageExpired;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName MessageExpiredConfig
 * @ Author Alex2
 * @ Date 2025/8/15 10:45
 **/
@Configuration
public class MessageExpiredConfig {
    // 正常交换机
    public static final String NORMAL_EXCHANGE_MESSAGE_EXPIRED = "normal_exchange_message_expired";
    public static final String NORMAL_QUEUE_MESSAGE_EXPIRED = "normal_queue_message_expired";
    public static final String NORMAL_ROUTING_KEY_MESSAGE_EXPIRED = "normal_routing_key_message_expired";

    // 死信交换机
    public static final String DLX_EXCHANGE_MESSAGE_EXPIRED = "dlx_exchange_message_expired";
    public static final String DLX_QUEUE_MESSAGE_EXPIRED = "dlx_queue_message_expired";
    public static final String DLX_ROUTING_KEY_MESSAGE_EXPIRED = "dlx_routing_key_message_expired";

    @Bean
    public DirectExchange normalExchangeMessageExpired() {
        return new DirectExchange(NORMAL_EXCHANGE_MESSAGE_EXPIRED);
    }

    @Bean
    public DirectExchange dlxExchangeMessageExpired() {
        return new DirectExchange(DLX_EXCHANGE_MESSAGE_EXPIRED);
    }

    // 绑定死信交换机
    @Bean
    public Queue normalQueueMessageExpired() {
        return QueueBuilder.nonDurable(NORMAL_QUEUE_MESSAGE_EXPIRED)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE_MESSAGE_EXPIRED)
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY_MESSAGE_EXPIRED)
                .build();
    }

    @Bean
    public Queue dlxQueueMessageExpired() {
        return new Queue(DLX_QUEUE_MESSAGE_EXPIRED, false);
    }

    @Bean
    public Binding bindingNormalQueueMessageExpired() {
        return BindingBuilder.bind(normalQueueMessageExpired())
                .to(normalExchangeMessageExpired())
                .with(NORMAL_ROUTING_KEY_MESSAGE_EXPIRED);
    }

    @Bean
    public Binding bindingDlxQueueMessageExpired() {
        return BindingBuilder.bind(dlxQueueMessageExpired())
                .to(dlxExchangeMessageExpired())
                .with(DLX_ROUTING_KEY_MESSAGE_EXPIRED);
    }
}